/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package keshe;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class IndexOfGrade extends IndexOrManage implements ActionListener{

    String[] grades={"","2018","2019","2020","2021"};
    JComboBox grade=new JComboBox(grades);
    JLabel la=new JLabel("选择查询年级");

    public IndexOfGrade(){
        super();
        p1.add(la);
        p1.add(grade);


        //注册监听
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        if(e.getActionCommand()=="显示"){
            text.setText("");

            if(grade.getSelectedItem().equals("")){
                JOptionPane.showMessageDialog(null, "找不到该年级", "Message",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else
                try{
                    Connection con=DataBaseCreate.getConnection();
                    Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                    //按年级查找数据库数据
                    ResultSet rs=sql.executeQuery("select * from student where Grade like '%"+grade.getSelectedItem()+"%'");//getSelectedItem()是JCombobox类的类成员函数，用于将字符串转换
                    text.append(grade.getSelectedItem()+"级学生信息:\r\n");
                    text.append("学号                姓名                性别                生日                专业                         年级                班别                语文                        英语                        数学                        总分\r\n");
                    while(rs.next()){
                        text.append(rs.getString("Num")+"           "+rs.getString("Name")+"           "+rs.getString("Sex")+"           "+rs.getString("Birth")+"           "+rs.getString("Major")
                                +"              "+rs.getString("Grade")+"                      "+rs.getString("Class")+
                                "                      "+rs.getFloat("Chinese")+"                      "+rs.getFloat("English")
                                +"                      "+rs.getFloat("Maths")+"                      "+rs.getFloat("Total")+"\r\n");
                    }


                }catch(java.lang.ClassNotFoundException err){
                    JOptionPane.showMessageDialog(null, err.getMessage(), "ClassNotFoundException:",
                            JOptionPane.INFORMATION_MESSAGE);
                }catch (SQLException ex){
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "SQLException: ", JOptionPane.INFORMATION_MESSAGE);
                }
        }
        else if(e.getActionCommand()=="取消"){
            setVisible(false);
        }
        else if(e.getActionCommand()=="保存"){
            TxtSave save=new TxtSave(text.getText(),(String) grade.getSelectedItem()+"级");
        }
    }

}

